package leetcode_day._2021._202106._0110;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/6/7 21:39
 * 目标和
 * 算法：动态规划
 * 假设 a 是正数集合，b 是负数集合
 * a + b = target
 * a - b = sum
 * 2a = target + sum => a = (target + sum) / 2
 * 这就转化成了使用 01 背包求组合
 */
public class _07_494 {

    public int findTargetSumWays(int[] nums, int target) {
        int sum = 0;
        for (int num : nums) sum += num;
        if (target > sum || (sum + target) % 2 != 0) return 0;
        target = (target + sum) >>> 1;
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for (int num : nums) {
            for (int j = target; j >= num; j--) dp[j] += dp[j - num];
        }
        return dp[target];
    }

}
